package com.cswnode.Service.impl;

import com.cswnode.Service.LikeService;
import com.cswnode.Util.Constant;
import com.cswnode.Util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

/**
 * @author csw
 * @create 2020/10/2 19:35
 */
@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void like(int userId, int entityType, int entityId,int EntityUserId) {
//        //生成key
//        String RedisKey = RedisKeyUtil.getRedisEntityKey(entityType,entityId);
//        //判断当前用户是否点过赞
//        if (redisTemplate.opsForSet().isMember(RedisKey,userId)){
//            //如果当前用户点过赞，则删除当前用户
//            redisTemplate.opsForSet().remove(RedisKey,userId);
//        }else {     //如果当前用户未点赞，则添加
//            redisTemplate.opsForSet().add(RedisKey,userId);
//        }
        //生成key
        String EntityKey = RedisKeyUtil.getRedisEntityKey(entityType,entityId);
        String UserKey = RedisKeyUtil.getRedisUserKey(EntityUserId);
        //开启事务
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                //查询当前用户是否点过赞
                Boolean isMember =  redisOperations.opsForSet().isMember(EntityKey,userId);
                //开始编程事务
                redisOperations.multi();
                //如果当前用户点过赞则，删除当前，用户被点赞的数量-1
                if (isMember){
                    redisOperations.opsForSet().remove(EntityKey,userId);
                    redisOperations.opsForValue().decrement(UserKey);
                }else {
                    redisOperations.opsForSet().add(EntityKey,userId);
                    redisOperations.opsForValue().increment(UserKey);
                }
                return redisOperations.exec();      //提交事务
            }
        });
    }


    @Override
    public long findEntityLikeCount(int entityType, int entityId) {
        String RedisKey = RedisKeyUtil.getRedisEntityKey(entityType,entityId);
        return redisTemplate.opsForSet().size(RedisKey);
    }

    @Override
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        String RedisKey = RedisKeyUtil.getRedisEntityKey(entityType,entityId);
        Boolean result = redisTemplate.opsForSet().isMember(RedisKey,userId);
        if (result){
            return Constant.USER_LIKE;
        }else {
            return Constant.USER_NOT_LIKE;
        }
    }

    @Override
    public int findUserLikeCount(int userId) {
        String RedisUserKey = RedisKeyUtil.getRedisUserKey(userId);
        Integer likeCount = (Integer) redisTemplate.opsForValue().get(RedisUserKey);
        return likeCount == null ? 0 : likeCount;
    }


}
